﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIS.Portal.Core.Base;
using BIS.Portal.Core.Global;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Utility.Converter;

namespace BIS.Portal.Modules.CMS
{
    public partial class SimpleMenu : ControlBase
    {
        #region Get Data Settings
        public string ClassName
        {
            get { return GetControlSetting<string>(ModuleSettingNames.SimpleMenu.txtClassName, PortalContext.LanguageAlias); }
        }
        public string ClassNameActive
        {
            get { return GetControlSetting<string>(ModuleSettingNames.SimpleMenu.txtClassActive, PortalContext.LanguageAlias); }
        }
        #endregion
        public string Cate_id
        {
            get
            {
                if (Request[ModuleQueryStringKey.CategoryId] != null)
                    return Functions.GetString(Request[ModuleQueryStringKey.CategoryId]);
                else
                {
                    return "";
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            rptMenuTop.DataSource = GenerateMenu(0,ClientCmsMenuBO.GetCategoriesForMenu(PortalContext.LanguageInUseId));
            rptMenuTop.DataBind();
        }
        private List<ClientCmsMenuEnity> GenerateMenu(int parentCategoryId,IList<ClientCmsMenuEnity> list)
        {
                List<ClientCmsMenuEnity> childCategories = new List<ClientCmsMenuEnity>();
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].ParentCategoryId == parentCategoryId)
                    {
                        childCategories.Add(list[i]);
                    }
                }
            return childCategories;
        }

        protected void rptMenuTop_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
            {
                ClientCmsMenuEnity objMenu = (ClientCmsMenuEnity) e.Item.DataItem;
                string sClassName = ClassName;
                bool temp = false;
                #region Url Của Category
                string url = url = WebHelpers.GetLink(ModuleQueryStringKey.UrlNhomTin, objMenu.CategoryPageAlias,
                    new string[] { ModuleQueryStringKey.CategoryId, ModuleQueryStringKey.CategoryName },
                    new string[] { objMenu.CategoryId.ToString(), Functions.UnicodeToKoDauAndGach(objMenu.CategoryName) });
                if (objMenu.CategoryType == 2)
                {
                    ClientCmsArticleIdByCategoryIdEntity objArticle =
                        ClientCmsArticleBO.GetArticleIdByCategoryId(PortalContext.LanguageInUseId,
                                                                    objMenu.CategoryId);
                    if (objArticle != null)
                    {
                        url = WebHelpers.GetLink(ModuleQueryStringKey.UrlLinkChiTiet, objArticle.PageAlias,
                                            new string[]
                                    {
                                   ModuleQueryStringKey.CategoryId,
                                   ModuleQueryStringKey.ArticleId,
                                   ModuleQueryStringKey.CategoryName,
                                   ModuleQueryStringKey.ArticleTile
                                   },
                                            new string[]
                                    {
                                   Convert.ToString(objMenu.CategoryId),
                                   Convert.ToString(objArticle.ArticleId),
                                   Functions.UnicodeToKoDauAndGach(objMenu.CategoryName),
                                   Functions.UnicodeToKoDauAndGach(objArticle.ArticleTitle)
                                   }
                        );
                    }
                }
                if(objMenu.UseDirectUrl==1)
                {
                    url = objMenu.CategoryDirectUrl;
                    if (PortalContext.PageAlias == objMenu.CategoryPageAlias)
                    {
                        temp = true;
                    }
                }
                #endregion
                #region Kiểm tra điề kiện để active menu
                if (Cate_id == Functions.GetString(objMenu.CategoryId))
                {
                    temp = true;
                }
                if (temp)
                {
                    sClassName = ClassNameActive;
                }
                #endregion
                HyperLink nav_link = e.Item.FindControl("NavLink") as HyperLink;
                if (nav_link != null)
                {
                    nav_link.NavigateUrl = url;
                    nav_link.Text = objMenu.CategoryName;
                    nav_link.Attributes.Add("class", sClassName);
                }
            }
             
        }

    }
}